<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\UsersResource;
use App\Services\AiZhanService;
use App\Services\EmailService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UsersController extends Controller
{
    /**
     * 修改用户信息
     * @param Request $request
     * @return void
     */
    public function ModifyUserInfo(Request $request)
    {
        $user = Auth::guard('api')->user();

        if($request->has('username') && !empty($request->username)){
            $user->username=$request->username;
        }
        if($request->has('email') && !empty($request->email)){
            $user->email=$request->email;
        }
        if($request->has('phone') && !empty($request->phone)){
            $user->phone=$request->phone;
        }
        if($request->has('password') && !empty($request->password)){
            // 密码加密

            $user->password=$request->password;
        }
        if($request->has('avatar') && !empty($request->avatar)){
            $user->avatar=$request->avatar;
        }
        if($user->save()){
            return $this->success('修改成功',UsersResource::make($user));
        }else{
            return $this->error('修改失败',UsersResource::make($user));
        }
   }
    public function sendEmail(Request $request)
    {
        $tomail=$request->input('tomail','');
        try {
            $result = app('email')->sendCode($tomail);
            if ($result){
                return $this->success('邮件发送成功');
            } else {
                return $this->error('邮件发送失败');
            }
        }  catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
   }
}
